package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.ResponseResult;
import com.example.model.entity.CurriculumInfo;
import com.example.model.entity.MemoInfo;
import com.example.service.CurriculumInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.dc.pr.PRError;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ling
 * @since 2024-08-18
 */
@RestController
@RequestMapping("/curriculum-info")
public class CurriculumInfoController {

    @Autowired
    private CurriculumInfoService curriculumInfoService;

    /**
     * 添加
     * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody CurriculumInfo curriculumInfo){
        curriculumInfo.setCreateTime(new Date());
        curriculumInfoService.save(curriculumInfo);
        return new ResponseResult();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseResult delete(Integer id){
        curriculumInfoService.removeById(id);
        return new ResponseResult();
    }

    /**
     * 更新
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody CurriculumInfo curriculumInfo){
        curriculumInfoService.updateById(curriculumInfo);
        return new ResponseResult();
    }



    /**
     * 获取列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(Integer page,Integer size,Integer userId,String curriculumName){
        if(!Objects.isNull(page)){
            PageHelper.startPage(page,size);
        }
        QueryWrapper<CurriculumInfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(curriculumName)){
            queryWrapper.like("curriculum_name","%"+curriculumName+"%");
        }
        queryWrapper.eq("user_id",userId);
        PageHelper.orderBy("create_time desc");
        PageInfo<CurriculumInfo> pageInfo = new PageInfo<>(curriculumInfoService.list(queryWrapper));
        List<CurriculumInfo> list = pageInfo.getList();
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("list",list);
        resultMap.put("total",pageInfo.getTotal());
        return new ResponseResult(resultMap);
    }
}
